﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bitcoin.Net
{
    public class NetEndPoint
    {
        public NetAddress Address { get; set; }

        public int Port { get; set; }

        public NetEndPoint(NetAddress address, int port)
        {
            Address = address;
            Port = port;
        }

        public static NetEndPoint Parse(string s)
        {
            var lastColon = s.LastIndexOf(':');
            var addressPart = s.Substring(0, lastColon);
            var portPart = s.Substring(lastColon + 1);
            var address = NetAddress.Parse(addressPart);
            var port = int.Parse(portPart);
            var endPoint = new NetEndPoint(address, port);
            return endPoint;
        }

        public override string ToString()
        {
            string s;
            if (Address.IsIPv4)
            {
                s = string.Format("{0}:{1}", Address, Port);
            }
            else
            {
                s = string.Format("[{0}]:{1}", Address, Port);                
            }
            return s;
        }
    }
}
